﻿layui.use(['form', 'table'], function () {
    let form = layui.form;
    let table = layui.table;
    let list_left = [];
    let list_right = [];
    let PATH = "../../";
    let param = getQueryStringByCN("param");
    let type = getQueryStringByCN("type");
    init();
    function init() {
        GetData();
        showTable2(list_right);
        LoadRole();
        LoadDept();
    };
    //search
    form.on('submit(formDemo)', function (data) {
        $.postJSON('api/role/GetUserByDeptAndCardNum', { deptName: $('#deptName').val(), serName: $('#serName').val() }, (res) => {
            if (res.code == 200) {
                list_left = RemoveArray(res.data, list_right);
                showTable(list_left);
            } else {
                layer.msg(res.msg);
            }
        })
        return false;
    });
    $("#add").click(function () {
        let checkStatus = table.checkStatus('demoTable');
        let data = checkStatus.data;
        list_right = MergeArray(list_right, data);
        showTable2(list_right);
        list_left = RemoveArray(list_left, list_right);
        showTable(list_left);
        if (list_right.length > 0 && $("#roleName").val() != "") {
            $('#btn_sub').removeClass('layui-btn-disabled');
            $('#btn_sub').removeAttr('disabled');
        }
    });
    $("#remove").click(function () {
        let checkStatus = table.checkStatus('demoTable2');
        let data = checkStatus.data;
        list_right = RemoveArray(list_right, data);
        list_left = MergeArray(list_left, data);
        showTable(list_left);
        showTable2(list_right);
        if (list_right.length < 1 || $("#roleName").val() == "") {
            $('#btn_sub').addClass('layui-btn-disabled');
            $('#btn_sub').attr('disabled', 'disabled');
        }
    });
    function MergeArray(arr1, arr2) {
        var _arr = new Array();
        for (var i = 0; i < arr1.length; i++) {
            _arr.push(arr1[i]);
        }
        for (var i = 0; i < arr2.length; i++) {
            var flag = true;
            for (var j = 0; j < arr1.length; j++) {
                if (arr2[i].cardNum == arr1[j].cardNum) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                _arr.push(arr2[i]);
            }
        }
        return _arr;
    }
    function RemoveArray(arr1, arr2) {
        let _arr = new Array();
        for (var i = 0; i < arr1.length; i++) {
            let flag = true;
            for (var j = 0; j < arr2.length; j++) {
                if (arr1[i].cardNum == arr2[j].cardNum) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                _arr.push(arr1[i]);
            }
        }
        return _arr;
    }
    $("#btn_sub").click(function () {

        if ($("#roleName").val() === "") {
            layer.msg("请选择角色");
            return false;
        }
        if (list_right.length === 0) {
            layer.msg("请选择用户");
            return false;
        }
        let list_kh = [];
        for (var i = 0; i < list_right.length; i++) {
            list_kh.push(list_right[i].cardNum);
        }
        list_kh = list_kh.join(',');

        var option = layui.jquery("#roleName option:selected");//获取当前选择项.
        $.postJSON('api/role/SetRole', { "CardList": list_kh.split(','), "RoleId": $("#roleName").val(), 'RoleName': option.text() }, (res) => {
            if (res.code == 200) {
                //重载
                var GLData = [];
                res.data.forEach(function (element) {
                    if (element.cardNum != "" && element.cardNum != "null") {
                        GLData.push(element);
                    }
                });
                list_left = RemoveArray(GLData, list_right);
                showTable(list_left);
            } else {
                layer.msg(res.msg);
            }
        })
    });

    form.on('select(deptfilter)', function (data) {
        $.postJSON('api/role/GetUserByDeptAndCardNum', { deptName: $('#deptName').val(), serName: $('#serName').val() }, (res) => {
            if (res.code == 200) {
                //重载
                var GLData = [];
                res.data.forEach(function (element) {
                    if (element.cardNum != "" && element.cardNum != "null") {
                        GLData.push(element);
                    }
                });
                list_left = RemoveArray(GLData, list_right);
                showTable(list_left);
            } else {
                layer.msg(res.msg);
            }
        })
    });
    form.on('select(deptfilter2)', function (data) {
        if (data.value == "" || list_right.length == 0) {
            $('#btn_sub').addClass('layui-btn-disabled');
            $('#btn_sub').attr('disabled', 'disabled');
        } else {
            $('#btn_sub').removeClass('layui-btn-disabled');
            $('#btn_sub').removeAttr('disabled');
        }
    });
    function GetData() {
        $.getData('api/role/GetAllUsing', (res) => {
            list_left = res.data
            showTable(list_left);
        })
    };
    function LoadRole() {
        $.getData('api/role/getAllRole', (res) => {
            if (res.code == 200) {
                $('#roleName').empty();
                $('#roleName').append(new Option("请选择", ""));
                $.each(res.data, function (index, item) {
                    $('#roleName').append(new Option(item.roleName, item.id));// 下拉菜单里添加元素
                });
                form.render("select");
            } else {
                layer.msg(res.msg);
            }
        })
    }
    function LoadDept() {
        $.getData('api/role/GetDeptUsing', (res) => {
            if (res.code == 200) {
                $('#deptName').empty();
                $('#deptName').append(new Option("请选择", ""));
                $.each(res.data, function (index, item) {
                    $('#deptName').append(new Option(item.departName, item.departName));// 下拉菜单里添加元素
                });
                form.render("select");
            } else {
                layer.msg(res.msg);
            }
        })
    }
    //组装表的数据
    function showTable(data) {
        table.render({
            elem: '#demoTable'
            //, toolbar: '#toolbar'
            , data: data
            , cols: [[
                { type: 'checkbox' }
                , { field: 'cardNum', title: '卡号', align: 'center' }
                , { field: 'name', title: '姓名', align: 'center' }
                , { field: 'departName', title: '部门', align: 'center' }
            ]]
            , page: true
            , limit: 12
            , size: 'sm'
            , done: function (res, curr, count) {
                //page_left = curr;
            }
        });
    }
    //组装表的数据
    function showTable2(data) {
        table.render({
            elem: '#demoTable2'
            //, toolbar: '#toolbar'
            , data: data
            , cols: [[
                { type: 'checkbox' }
                , { field: 'cardNum', title: '卡号', align: 'center' }
                , { field: 'name', title: '姓名', align: 'center' }
                //, { field: 'Dept', title: '部门', align: 'center' }
            ]]
            , page: true
            , limit: 12
            , size: 'sm' //小尺寸的表格
            //, done: function (res, curr, count) {}
        });
    }
});